USE Lib
GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
----------------------------
IF EXISTS (SELECT * FROM sysobjects WHERE name = 'fn_iif') BEGIN
	DROP  FUNCTION  tsql.fn_iif
	PRINT '<<< DROP FUNCTION tsql.fn_iif - Completed with SUCCESS >>>'
END

GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*================================================================================================
Name:			tsql.fn_iif
Type:			User defined function 
Owner:			
Description:	TSql implementation of VB IIF function 
Parameters:		@condition = logical condition to be evaluated
				@value4true = return value for true condition
				@value4false = return value for false condition
Dependens on:  	none
Usage:			SELECT tsql.fn_iif(1=1, 'true', 'false')
--------------------------------------------------------------------------------------------------
Project:		http://sqllib.codeplex.com/
Help:			http://sqllib.codeplex.com/wikipage?title=fn_iif&referringTitle=Documentation		
Version:		20120822
SrcCtrlApp:		SVN
SrcCtrlPath:	http://sqllib.codeplex.com/SourceControl/list/changesets
--------------------------------------------------------------------------------------------------
History:		07/23/2012 - Adrian E Dudau - Function created
History:		08/22/2012 - Adrian E Dudau - Added references to project, SVN and Help
================================================================================================*/

CREATE FUNCTION [tsql].[fn_iif] 
	(@condition bit 
	,@value4true sql_variant
	,@value4false sql_variant  
	)
RETURNS sql_variant AS
BEGIN

	IF ISNULL(@condition,0) = 1   
		RETURN @value4true

	-- ELSE
	RETURN @value4false 		
END /*FUNCTION*/

GO

---------------------------------------------------------------------------
-- BEGIN MAINTENANCE WRAPPER
-- Verify completion
----------------------------

IF EXISTS(SELECT * FROM sysobjects WHERE name = 'fn_iif') BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_iif - Completed with SUCCESS >>>'
END

ELSE BEGIN 
	PRINT '<<< CREATE FUNCTION tsql.fn_iif - Completed with ERROR >>>'
END


GO
----------------------------
-- END MAINTENANCE WRAPPER
---------------------------------------------------------------------------

-- GRANT SELECT TO PUBLIC will be used at Db level



